<?php
/**
 * @特惠赚、短信控制器
 * Author: 小谢
 * Date: 2019-5-8
 */
namespace app\admin\controller; 
use think\Controller;
use think\Db;
use think\facade\Session;
use think\facade\View;

class Sms extends Base {

   /**
    * @brief 短信配置
    */
    public function config() {
      $data =  I('post.');
      if(isset($data['id']) && $data['id'] > 0) {
            $data['add_time'] = time();
            $res = M('sms_config')->where(['id'=>$data['id']])->update($data);
            if($res) {
                $this->success("操作成功",'/Admin/sms/config');
            }
            $this->success("操作失败",'/Admin/sms/config');
      }
      $info = M('sms_config')->find();
      View::share(['info'=>$info]);
      return View();
    }

   /**
    * @brief 短信模板
    */
    public function template() {
        $list = M('sms_template')->select();
        $scene = $this->sendScene();
        $sms_platform = M('sms_config')->column('sms_platform');
        View::share(['list'=>$list,'scene'=>$scene,'sms_platform'=>$sms_platform]);
        return View();
    }

   /**
    * @brief 短信信息
    */
    public function sms_info() {
       $sceneList = $this->sendScene();
       $tpl_id  = I('get.tpl_id');
       if($tpl_id) {
            $info = M('sms_template')->where(['tpl_id'=>$tpl_id])->find();
            View::share(['info'=>$info]);
       }
       View::share(['sceneList'=>$sceneList]);
       return View();
    }
   
   /**
    * @brief 操作
    */
    public function saveHandle() {
        $data = I('post.data');
        $validate = new \app\admin\validate\Sms();
        if (!$validate->check($data)){
              $this->error($validate->getError(),'/Admin/sms/template');
        } else {
            if($data['tpl_id']) {
                $res = M('sms_template')->where(['tpl_id'=>$data['tpl_id']])->update($data);
            } else {
                $res = M('sms_template')->add($data);
            }
            if($res){
                $this->success('操作成功','/Admin/sms/template');
            }
            $this->success('操作失败','/Admin/sms/template');
        }
    }
    
     /**
     * 删除一条
     */
    public function del(){
        $id = I('post.tpl_id','');
        empty($id) &&  $this->ajaxReturn(['status' => -1,'msg' =>"非法操作！",'data'  =>'']);
        DB::name('sms_template')->where('tpl_id',$id)->delete();
        $this->ajaxReturn(['status' => 1]);
    }


}